Suggested title: Efficient Morse for Microcontrollers

So... This author was looking over the source code for a microcontroller
controlled CW beacon, programmed by an influential Australian digital radio
mode designer who shall not be named, and their Morse Code converter used...
character strings. I mean this sort of thing:

<use a monospace font>
    char* morsetable[] = {".-", "-...", "-.-.", "-..", "." };

All those wasted bytes, all the unnecessary code to process it..
Encoding Morse this way on small microcontrollers makes the baby Alan
Turing cry.

The better method that I want to describe, and if I fail at that, you should
just blindly copy the code below anyway, is particularly efficient on memory
use and processing from the CPU's point of view. It has been no secret to
erudite microcomputer programmers, there being an article written by
Lawrence Krakauer in the October 1976 issue of BYTE Magazine[biblio 1].

This algorithm has the Morse encoded into a single byte, and that's
obviously converting the dits and dahs into 0 and 1 bit values. The letter V
will be "0001", the letter K is "101", and so on. To transmit a symbol with
that encoding, you're checking the left-most bit in the sequence, deciding
to do a dit or a dah, forgetting that bit, and repeating the process until
you're bitless.

There's an immediate issue here: when working with a small binary value like
"0001", computers will want to consider it as the 8-bit byte "00000001". So
the length of the bit sequence needs to be provided without wasting memory.

And this is what is done: add a "1" to the end of the sequence, so the
"0001" becomes "00011", and right-fill the bits to complete the byte,
producing "00011000".

"But won't that 'end' bit be mistaken for a dah, and there's those extra
dits as well?" Now for the programming trick: when using a shift instruction
to move the bits in a CPU register to the left, a zero bit is inserted from
the right, and the pushed-off left-most bit's value is copied into the CPU
Carry register. Then while keying out the morse code, the program can detect
when the correct number of dits and dahs have been sent when the result of
the shift instruction is zero, and the Carry has been set to '1' from that
additional bit: the first and only time both conditions can occur.

If the result of that shift instruction is non-zero, the value of the
Carry decides on sending a dit or dah, and the process jumps back to the
shift instruction.

This is obviously an algorithm from 1976 that is best implemented with the
toggle switches on a real computer's front panel, as the high-level
languages of today aren't very accommodating in accessing the CPU Carry
register directly. For those programs, and also microprocessors with 16 bit
or larger registers that have issues manipulating bytes directly, performing
the shift operation on the entire register, then testing for zero in only
the least-significant 8 bits could be done instead.

Now for the tricky public domain-licensed C code I want you all to cut and
paste into your own software. Those of you reading the paper edition of AR
will need to TYPE IT IN, just like in the old days...

<font="Monospace" baby-alan-prefers="Courier, like in the K&R his parents
read to him at night">

#include <stdio.h>
#define dah_carrier() putchar('-')
#define dit_carrier() putchar('.')
#define dit_space() putchar('\0')
#define dah_space() putchar(' ')

const unsigned char morsetable[] =
  { 128, 174,  74, 128,  19, 128,  68, 122, 180, 182, 128,  84,
    206, 134,  86, 148, 252, 124,  60,  28,  12,   4, 132, 196,
    228, 244, 226, 170, 128, 140, 128,  50, 106,  96, 136, 168,
    144,  64,  40, 208,   8,  32, 120, 176,  72, 224, 160, 240,
    104, 216,  80,  16, 192,  48,  24, 112, 104, 184, 200 };

void morseout (char *s) {
  unsigned short b, c;
  for (; *s; s++) {
    c = *s - 32;
    if (c < 59)
      for (b = morsetable[c]; (b <<= 1) & 255; dit_space())
        if (b & 256) dah_carrier(); else dit_carrier();
    dah_space();
  }
}

void main (void) { morseout ("VK2CJB"); }

</font>

Bibliography

1. Lawrence Krakauer, (1976) 'Efficient Storage of Morse Character
Codes', BYTE, Issue 14, pages 36-38,
https://archive.org/details/byte-magazine-1976-10
